<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body style="height: 2000px">
    <script>
      //   let flag;
      //   function debounce(fn, delay) {
      //     if (flag) {
      //       clearTimeout(flag);
      //     }
      //       flag = setTimeout(fn, delay);
      //
      //   }

      function scrollTop() {
        var scrollTop = document.documentElement.scrollTop;
        console.log("滚动条位置：" + scrollTop);
      }
      //   window.onscroll = () => {
      //     debounce(scrollTop, 200);
      //   };

      function debounce(fn, delay) {
        let flag;
        return () => {
          if (flag) {
            clearTimeout(flag);
          }
          flag = setTimeout(fn, delay);
        };
      }

      window.onscroll = debounce(scrollTop, 200);
    </script>
  </body>
</html>
